const { json } = require('express')
const express = require('express')
const app = express()
const fs = require('fs')

const filterJson = require('./api/filters')

const jsonWrite = function (res,data,err) {
  if (!err) {
    res.writeHead(200, { "Content-Type" : "text/html;charset=utf-8" })
    res.end(data)
  } else {
    throw err
  } 
}


// 轮播图
app.get('/api/index/swiper', (req, res) => {
  fs.readFile('./json/swiper.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})

// 文字数据
app.get('/api/index/navigator', (req, res) => {
  fs.readFile('./json/navigator.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})

// 新闻
app.get('/api/index/notice', (req, res) => {
  fs.readFile('./json/notice.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})

// 限时秒杀
app.get('/api/index/miaosha', (req, res) => {
  fs.readFile('./json/miaosha.json',(err,data) => {
    jsonWrite(res,req,data,err)
  })
})

 // 爆品推荐
 app.get('/api/index/recommend', (req, res) => {
  fs.readFile('./json/recommend.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})

// 商品列表
app.get('/api/index/goods', (req, res) => {
  fs.readFile('./json/goods.json',(err,data) => {
    filterJson.searchSeckill(req,res,data,err)
  })
})

// 商品导航
app.get('/api/index/categories', (req, res) => {
  fs.readFile('./json/categories.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})

// 好礼直播
app.get('/api/index/live', (req, res) => {
  fs.readFile('./json/live.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})

// 公告详情
app.get('/api/index/detail', (req, res) => {
  fs.readFile('./json/detail.json',(err,data) => {
    jsonWrite(res,data,err)
  })
})


app.listen(8080,() => console.log('8080'))